import { reactive } from "vue";
import { Api } from "../../../api/api.js";
import { ElNotification } from 'element-plus';
import { GJ_nowTime } from "../../../../static/js/tool.js";
function expenseAddJS() {
    let status = reactive({
        userinfo: JSON.parse(window.sessionStorage.getItem("userinfo")),
        expenseAdd: {
            //消费类型
            expenseType: "",
            //消费金额
            expenseMoney: "",
            //消费描述
            expenseDesc: "",
        },
        //消费类型数据
        expenseTypeData: [
            {
                label: "日常消费",
                value: "日常消费",
            },
            {
                label: "生活消费",
                value: "生活消费",
            },
            {
                label: "网上购物",
                value: "网上购物",
            },
            {
                label: "学习消费",
                value: "学习消费",
            },
            {
                label: "游玩消费",
                value: "游玩消费",
            },
            {
                label: "交通出行",
                value: "交通出行",
            },
            {
                label: "其他",
                value: "其他",
            },
        ]
    })
    let methodFun = {
        //提交消费记录
        submitFun: () => {
            if ((status.expenseAdd.expenseType != "") & (status.expenseAdd.expenseMoney != "")) {
                let data = {
                    user: "",
                    item: [
                        {
                            yearDate: "",
                            yearDetails: [
                                {
                                    monDate: "",
                                    monDetails: [
                                        {
                                            dayDate: "",
                                            dayDetails: [
                                                {
                                                    createTime: "",
                                                    money: "",
                                                    type: "",
                                                    desc: ""
                                                },
                                            ],
                                        },
                                    ],
                                },
                            ],
                        },
                    ],
                };
                data.user = status.userinfo.user;
                data.item[0].yearDate = GJ_nowTime("year", "-");
                data.item[0].yearDetails[0].monDate = `${GJ_nowTime("year", "-")}-${GJ_nowTime("mon", "-")}`;
                data.item[0].yearDetails[0].monDetails[0].dayDate = GJ_nowTime("date", "-");
                data.item[0].yearDetails[0].monDetails[0].dayDetails[0].createTime = GJ_nowTime("date-time", "-");
                data.item[0].yearDetails[0].monDetails[0].dayDetails[0].money =
                    status.expenseAdd.expenseMoney;
                data.item[0].yearDetails[0].monDetails[0].dayDetails[0].type =
                    status.expenseAdd.expenseType;
                data.item[0].yearDetails[0].monDetails[0].dayDetails[0].desc = status.expenseAdd.expenseDesc;
                console.log(data, "submit");
                Api.expenseAdd(data).then((res) => {
                    if (res.data) {
                        ElNotification({
                            type: "success",
                            title: "添加成功",
                            message: "消费列表已更新",
                            duration: 1000,
                        });
                        methodFun.init();
                    }
                });
            } else {
                ElNotification({
                    type: "success",
                    title: "记录失败",
                    message: "信息填写不完整",
                    duration: 1000,
                });
            }
        },
        //初始化表单
        init:()=>{
            status.expenseAdd.expenseType = "";
            status.expenseAdd.expenseMoney = "";
            status.expenseAdd.expenseDesc = "";
        }
    }
    return {
        status,
        methodFun
    }
}
export default expenseAddJS;